package cn.yuli.redis.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.yuli.redis.mapper.UserMapper;
import cn.yuli.redis.model.User;
import cn.yuli.redis.service.UserService;
//@Service
public class UserServiceImpl2 implements UserService {
	public static Logger logger = Logger.getLogger(UserServiceImpl2.class);
	@Autowired
	private UserMapper userMpper;//usermapper接口
	@Transactional
	@CachePut(key="#user.id",value="userCache")
	public User addUser(User user) {
		userMpper.insert(user);
		logger.info("向数据库添加用户");
		return user;
	}
	@Transactional
	@CacheEvict(key="#userId",value="user")
	public void deleteUser(String userId) {
		userMpper.deleteByPrimaryKey(userId);
		
	}
	@Transactional
	@CachePut(key="#user.id",value="user")
	public User updateUser(User user) {
		userMpper.updateByPrimaryKeySelective(user);
		return user;
	}
	@Transactional(readOnly=true)
	@Cacheable(key="#userId",value="user")
	public User queryUser(String userId) {
		logger.info("从数据库里取得用户");
		User user = userMpper.selectByPrimaryKey(userId);
		return user;
	}

}
